#include "count.h"
#include "gc.h"
#include "test.h"
#include <cstdlib>
#include <iostream>
#include <thread>

Ptr ptr0(false, true);
TEST(Func, MutiThread) {
  auto func = [] {
    Ptr ptr1(true, false), ptr2, ptr3, ptr4, ptr5, obj6;
    ptr1.PointTo(ptr3);
    ptr3.PointTo(obj6);
    ptr4.PointTo(ptr5);
    ptr3.PointTo(obj6);
    ptr4.PointTo(ptr5);
    ptr5.PointTo(ptr4);
  };
  auto gc_run = [] { GC::GetInstance().Run(); };
  std::thread thread(gc_run);
  func();
  thread.join();
}

TEST(Func, Cycle) {
  Ptr ptr1, ptr2, ptr3;
  ptr1.PointTo(ptr2);
  ptr2.PointTo(ptr3);
  ptr3.PointTo(ptr1);
  GC::GetInstance().Run();
}

END(Calc){
  auto map_result = count.getCount();

  auto is_err=false;
  for (auto iter : map_result) {
    auto val = iter.second;
    std::cout << "id:" << val.id << " isSta:" << val.isStack
              << " isGlo:" << val.isGloab << " isDel:" << val.isDel
              << " delway:" << val.delWay
              << " isLeft:" << (val.isGloab ? 0 : !val.isDel) << std::endl;
    if (!val.isGloab&&!val.isDel) {
      is_err = true;
    }
  }
  if (is_err) {
    std::cout << "error" << std::endl;
    exit(-1);
  } else {
    std::cout << "success" << std::endl;
    exit(0);
  }
}

